package com.maiji.cloud.controller.login;

import com.maiji.cloud.controller.BaseController;
import com.maiji.cloud.entities.login.Label;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.login.FoodReqData;
import com.maiji.cloud.request.login.FoodFindAllReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.FoodMaterialResData;
import com.maiji.cloud.response.login.FoodTypeResData;
import com.maiji.cloud.response.shopingmall.GoodsResData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@RestController
@RequestMapping("/food/")
@Api(tags = { "食材模块" })
public class FoodController extends BaseController {

	/**
	 * 根据标签查询食材列表
	 * @param baseDataReqDto
	 * @return
	 */
	@ApiOperation(value = "根据标签获取食材列表")
	@PostMapping("findAllFoods")
	public BaseMetaResDto<List<FoodMaterialResData>> findAllFoods(@RequestBody BaseDataReqDto<FoodFindAllReqData> baseDataReqDto) {
		return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/findAllFoods", baseDataReqDto, BaseMetaResDto.class);
	}

    /**
     *  获取食材分类分级列表
     * @return
     */
    @ApiOperation(value = "获取食材分类分级列表")
    @PostMapping("findAllFoodTypes")
    public BaseDataResDto<List<FoodTypeResData>> findAllFoodTypes(@RequestBody BaseDataReqDto<Boolean> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/findAllFoodTypes", baseDataReqDto, BaseDataResDto.class);
    }

    /**
     *  编辑食材分类分级
     * @return
     */
    @ApiOperation(value = "编辑食材分类分级")
    @PostMapping("editFoodType")
    public BaseResDto editFoodType(@RequestBody BaseDataReqDto<FoodTypeResData> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/editFoodType", baseDataReqDto, BaseResDto.class);
    }

    /**
     *  批量逻辑删除食材
     * @return
     */
    @ApiOperation(value = "批量逻辑删除食材")
    @PostMapping("delFoodsByIds")
    public BaseResDto delFoodsByIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/delFoodsByIds", baseDataReqDto, BaseResDto.class);
    }

    /**
     *  添加食材
     * @return
     */
    @ApiOperation(value = "添加食材")
    @PostMapping("addFood")
    public BaseResDto addFood(@RequestBody BaseDataReqDto<FoodReqData> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/addFood", baseDataReqDto, BaseResDto.class);
    }

    /**
     *  编辑食材
     * @return
     */
    @ApiOperation(value = "编辑食材")
    @PostMapping("editFood")
    public BaseResDto editFood(@RequestBody BaseDataReqDto<FoodReqData> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/editFood", baseDataReqDto, BaseResDto.class);
    }

    /**
     *  添加食材
     * @return
     */
    @ApiOperation(value = "获取食材详情")
    @PostMapping("findFoodById")
    public BaseMetaResDto<FoodMaterialResData> findFoodById(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/getFoodMaterialDetailById", baseDataReqDto, BaseMetaResDto.class);
    }

    /**
     * 根据食材ID获取食材的标签列表
     * @param baseDataReqDto
     * @return
     */
    @ApiOperation(value = "根据食谱ID获取食谱的标签")
    @PostMapping("findAllLabelsByFoodId")
    public BaseDataResDto<List<Label>> findAllLabelsByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/findAllLabelsByFoodId", baseDataReqDto, BaseDataResDto.class);
    }

    /**
     *  获取食材相关商品列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllGoodsByFoodId")
    @ApiOperation(value = "根据食材ID获取食材相关商品列表")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto){
        return restTemplate.postForObject(REST_URL_APPLOGIN + "/deit/findAllGoodsByFoodId", baseDataReqDto, BaseMetaResDto.class);
    }

    /**
     *  批量导入食材
     * @param uploadFile
     * @return
     */
    @PostMapping("uploadFoods")
    @ApiOperation(value = "批量导入食材")
    public BaseDataResDto<String> uploadFoods(@RequestBody MultipartFile uploadFile) {
        try {
            return postForObjectWithMultipart(uploadFile, REST_URL_APPLOGIN + "/deit/uploadFoods", BaseDataResDto.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseDataResDto(Status.ERROR);
        }
    }

}
